import permision from "@/js_sdk/wa-permission/permission.js"

var isIOS
// #ifdef APP-PLUS
isIOS = (plus.os.name == "iOS")
// #endif

// 检查是否开启位置信息权限
export function checkOpenPermission() {
	return new Promise(async (resolve, reject) => {
		let result
		if (isIOS) {
			result =permision.judgeIosPermission("location")
		} else {
			result = await permision.requestAndroidPermission(
				'android.permission.ACCESS_FINE_LOCATION');
		}
		let flag = false
		if (isIOS) {
			flag = result
		} else {
			flag = result == 1 ? true : false
		}
		console.log("result===", result)
		if (flag) {
			console.log("已获得授权");
			resolve()
		} else {
			console.log("未获得权限");
			if (result === -1||(isIOS&&!result)) {
				uni.showModal({
					title: '提示',
					content: '请开启位置信息权限',
					showCancel: false,
					success() {
						permision.gotoAppPermissionSetting(); // 打开权限设置界面
					}
				});
			}
			reject()
		}
	})

}


// 检查是否开启位置信息服务
export function checkOpenService() {
	return new Promise(async (resolve, reject) => {
		let result = permision.checkSystemEnableLocation();
		if (result) {
			console.log('已开启定位服务功能');
			resolve()
		} else {
			uni.showModal({
				title: '提示',
				content: '请打开定位服务功能',
				showCancel: false,
				success() {
					if (isIOS) {
						permision.gotoAppPermissionSetting(); // 打开权限设置界面
					} else {
						var main = plus.android.runtimeMainActivity();
						var Intent = plus.android.importClass('android.content.Intent');
						var Settings = plus.android.importClass('android.provider.Settings');
						var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						main.startActivity(intent); // 打开系统设置GPS服务页面
					}
				}
			});
			reject()
		}

	})

}


const authOptions = {
	"camera": {
		permissionID: "android.permission.camera",
		content: "检测到您没打开相机权限，是否去设置打开？"
	},
	"photoLibrary": {
		permissionID: "android.permission.WRITE_EXTERNAL_STORAGE",
		content: "检测到您没打开相册权限，是否去设置打开？"
	},
	"": "",
	"": "",
	"": "",
	"": "",
}

// 检查是否开启权限
export function checkPermision({
	permissionID = "photoLibrary",
	content = "检测到您没打开相册权限，是否去设置打开？"
}) {
	return new Promise(async (resolve, reject) => {

		let flag = null
		let result = null
		let modalContent = content
		if (isIOS) {
			result = await permision.judgeIosPermission(permissionID);
			flag = result
		} else {
			result = await permision.requestAndroidPermission(authOptions[permissionID].permissionID);
			modalContent = authOptions[permissionID].content
			flag = result === -1 ? true : false
			result = result === 1 ? true : false
		}
		if (!result) {
			if (flag) {
				uni.showModal({
					title: '提示',
					content: modalContent,
					showCancel: true,
					cancelText: '取消',
					confirmText: '去设置',
					success() {
						permision.gotoAppPermissionSetting(); // 打开权限设置界面
					}
				});
			}
			reject()
		} else {
			console.log('已获得权限');
			resolve()
		}
	})

}
